package com.campus.lostandfound.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.campus.lostandfound.common.Result;
import com.campus.lostandfound.entity.ItemInfo;
import com.campus.lostandfound.service.ItemInfoService;
import com.campus.lostandfound.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 失物/招领信息Controller
 */
@RestController
@RequestMapping("/item")
@CrossOrigin
public class ItemInfoController {
    
    @Autowired
    private ItemInfoService itemInfoService;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    /**
     * 发布信息
     */
    @PostMapping("/publish")
    public Result<String> publishInfo(@RequestHeader("Authorization") String token,
                                      @RequestBody ItemInfo itemInfo) {
        try {
            String userId = jwtUtil.getUserIdFromToken(token);
            itemInfo.setPublisherId(userId);
            
            itemInfoService.publishInfo(itemInfo);
            return Result.success("发布成功，等待审核");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 分页查询信息
     */
    @PostMapping("/list")
    public Result<IPage<ItemInfo>> pageQuery(@RequestBody Map<String, Object> params) {
        try {
            // 安全的类型转换
            Object currentObj = params.getOrDefault("current", 1);
            Object sizeObj = params.getOrDefault("size", 10);
            
            Integer current = currentObj instanceof Number ? ((Number) currentObj).intValue() : 1;
            Integer size = sizeObj instanceof Number ? ((Number) sizeObj).intValue() : 10;
            
            IPage<ItemInfo> page = itemInfoService.pageQuery(current, size, params);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取信息详情
     */
    @GetMapping("/{id}")
    public Result<ItemInfo> getInfo(@PathVariable Long id) {
        try {
            ItemInfo itemInfo = itemInfoService.getById(id);
            if (itemInfo == null) {
                return Result.error("信息不存在");
            }
            return Result.success(itemInfo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取我的发布
     */
    @GetMapping("/my")
    public Result<IPage<ItemInfo>> getMyPublish(@RequestHeader("Authorization") String token,
                                                @RequestParam(defaultValue = "1") Integer current,
                                                @RequestParam(defaultValue = "10") Integer size) {
        try {
            String userId = jwtUtil.getUserIdFromToken(token);
            IPage<ItemInfo> page = itemInfoService.getMyPublish(userId, current, size);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除信息
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteInfo(@RequestHeader("Authorization") String token,
                                     @PathVariable Long id) {
        try {
            String userId = jwtUtil.getUserIdFromToken(token);
            itemInfoService.deleteInfo(id, userId);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 审核信息（管理员）
     */
    @PostMapping("/audit")
    public Result<String> auditInfo(@RequestBody Map<String, Object> params) {
        try {
            Long id = Long.valueOf(params.get("id").toString());
            Integer status = (Integer) params.get("status");
            String auditComment = (String) params.get("auditComment");
            
            itemInfoService.auditInfo(id, status, auditComment);
            return Result.success("审核成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

